package com.tbit.uqbike.service.event.listener;

import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.service.event.eventItem.BrandRemoteConfigEvent;
import com.tbit.uqbike.service.event.EventType;
import com.tbit.uqbike.service.event.handler.EventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 品牌远程配置事件监听器
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/5/16
 */
@Component
@Slf4j
public class BrandRemoteConfigListener {

    @Autowired
    private List<EventHandler> eventHandlers;


    @EventListener
    public void onBrandRemoteConfig(BrandRemoteConfigEvent event) {
        List<BrandRemoteConfig> brandRemoteConfigs = (List<BrandRemoteConfig>) event.getSource();
        Assert.notEmpty(brandRemoteConfigs, "brandRemoteConfigs must not be empty");
        log.info("品牌远程配置事件监听器收到事件：{}", event);

        if (CollectionUtils.isEmpty(eventHandlers)) {
            return;
        }
        EventType eventType = event.getEventType();
        for (EventHandler eventHandler : eventHandlers) {
            brandRemoteConfigs.forEach(brandRemoteConfig -> eventHandler.handle(brandRemoteConfig, eventType));
        }
    }

}